<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    <title>中心管理</title>
    <link rel="stylesheet" href="../../dist/css/bootstrap.min.css">
    <link href="../../css/bootstrap-datetimepicker.css" rel="stylesheet"/>
    <link rel="stylesheet" href="../../css/base.css">
    <link rel="stylesheet" href="../../css/all-like-orderlist.css">
    <link rel="stylesheet" href="../../css/people.css">
    <link rel="stylesheet" href="../../css/main.css">
    <link rel="stylesheet" href="../../css/jquery.pagination.css">
    <script src="../../js/jquery.min.js"></script>
    <script src="../../dist/js/bootstrap.js"></script>
    <!--引入时间插件-->
    <script src="../../js/moment-with-locals.min.js"></script>
    <script src="../../js/bootstrap-datetimepicker.js"></script>
    <script>
        $(document).ready(function () {
            $("#datetimepicker").datetimepicker({
                format: "yyyymm",
                weekStart: 1,
                autoclose: true,
                startView: 3,
                minView: 3,
                forceParse: false,
                endDate: new Date(), // 窗口可选时间从今天开始
                pickerPosition: "bottom-left"
            });
            $("#datetimepicker2").datetimepicker({
                format: "yyyymm",
                weekStart: 1,
                autoclose: true,
                startView: 3,
                minView: 3,
                forceParse: false,
                endDate: new Date(), // 窗口可选时间从今天开始
                pickerPosition: "bottom-left"

            });
            //批量删除，获得批量编号
            $(".delbtn").click(function () {
                var ids = BBB('ck');
                if (ids.length > 0) {
                    $.ajax({
                        url: "/system/del_ids",
                        contentType: "application/json; charset=utf-8",
                        type: "post",
                        data: ids,
                        success: function (data) {
                            if (data.code == "0") {
                                alert("删除成功!")
                                window.location.href = "/system/center";
                            } else {
                                alert("删除失败!");
                            }
                        }
                    });
                }
            });

            $("#update").click(function () {
                var centerInfo = {
                    id: $("#center-update-id").val(),
                    centerName: $("#recipient-name").val(),
                    centerAddress: $("#recipient-address").val(),
                };
                $.ajax({
                    url: "/system/addCenter",
                    contentType: "application/json; charset=utf-8",
                    type: "post",
                    data: JSON.stringify(centerInfo),
                    success: function (data) {

                        if (data.code == "0") {
                            window.location.href = "/system/center";
                        } else {
                            $("#updateText").css("color", "red");
                            $("#updateText").html(data.msg);
                        }
                    }
                });
            });
            $("#add_model").click(function () {
                $("#center-name").val("");
                $("#center-address").val("");
                $("#addText").html("");
            });

            //新增
            $("#add").click(function () {
                var centerInfo = {
                    centerName: $("#center-name").val(),
                    centerAddress: $("#center-address").val(),
                };
                $.ajax({
                    url: "/system/addCenter",
                    contentType: "application/json; charset=utf-8",
                    type: "post",
                    data: JSON.stringify(centerInfo),
                    success: function (data) {

                        if (data.code == "0") {
                            window.location.href = "/system/center";
                        } else {
                            $("#addText").css("color", "red");
                            $("#addText").html(data.msg);
                        }
                    }
                });
            });
            $("#query").click(function () {
                var centerPage = {
                    centerName: $(".select-option .order-num").val(),
                    size: 5,
                };
                $.ajax({
                    url: "/system/center",
                    contentType: "application/json; charset=utf-8",
                    type: "post",
                    data: JSON.stringify(centerPage),
                    success: function (data) {
                        $("#tb tr").html("");
                        var tb = "";
                        $.each(data.list, function (n, v) {

                            tb += "<tr id='tr'>" +
                                "<td><input name='ck'type='checkbox' value='" + v.id + "'/></td>" +
                                "<td>" + v.centerName + "</td>" +
                                "<td>" + v.centerAddress + "</td><td>";
                            $.each(v.stores,function (a,b) {
                                tb+="<span>"+b.storesName+"</span>&nbsp;";
                            })

                            tb+= "</td><td><img onclick='lick(this,this.id)' src='../../images/delete02.png'/><img src='../../images/edit.png'data-toggle=\"modal\"\n" +
                                "                            data-target='#exampleModal'" +
                                "                            onclick='updateClick(this, this.id)' " +
                                "                            data-whatever='修改洗衣门店信息'/></td>" +
                                "</tr>"

                        });
                        $("#tb").html(tb);
                        if ($("#tb").html()==""){
                            tb = "<tr><td colspan='5'>没有查询到数据!</td></tr>"
                            $("#tb").html(tb);
                            $("#tb tr td").css({"height":"100px","font-size":"20px","color":"red","text-align":"center","line-height":"100px"});
                        }
                    }
                });
            });
        })
    </script>
</head>
<body>
<div class="container-fluid">
    <div class="solid-hr row"></div>
    <div class="operation row">
        <button class="btn delete delbtn col-md-1"><img src="../images/delete01.png" alt="">批量删除</button>
        <button id="add_model" data-toggle="modal" data-target="#addModal" class="btn btn-success distribute col-md-1">
            <img
                    src="../images/add.png" alt="">添加
        </button>
        <form class="select-option col-md-6">
            <input class="order-num" type="text" placeholder="请输入用户名">
            <input id="query" type="button" class="btn btn-success sub" value="查询">
        </form>
    </div>
    <!--表格-->
    <div class="table-list row">
        <table class="table table-bordered">
            <tr class="active">
                <td><input onclick="AAA('ck')" type="checkbox" checked></td>
                <td>干洗中心名称</td>
                <td>地址</td>
                <td>下属门店</td>
                <td>操作</td>
            </tr>
            <tbody id="tb">
            <tr th:each="p : ${pagination.list}">
                <td><input name="ck" type="checkbox" th:value="${p.id}"></td>
                <td th:text="${p.centerName}">hys20180716</td>
                <td th:text="${p.centerAddress}">张三</td>
                <td>
                    <span th:each="s : ${p.stores}" th:text="${s.storesName}"></span>
                </td>
                <td><img onclick="lick(this, this.id)" src="../images/delete02.png"/><img src="../images/edit.png" data-toggle="modal"
                                                            data-target="#exampleModal" onclick="updateClick(this, this.id)"
                                                            data-whatever="修改用户信息"/></td>
            </tr>
            </tbody>
        </table>
        <span th:text="${pagination.totalPage}" id="totalPage" style="display: none;"></span>
    </div>
    <!--分页-->
    <div class="page-right">
        <div id="pagination" class="page fl"></div>
    </div>

    <!--新增弹框-->
    <div class="modal fade" id="addModal" tabindex="-1" role="dialog" aria-labelledby="addModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                            aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="addModalLabel">新增洗衣中心</h4>
                </div>
                <div class="modal-body">
                    <form>
                        <div class="form-group">
                            <label for="center-name" class="control-label"><i>*</i>干洗中心名称</label>
                            <input type="text" class="form-control" id="center-name">
                        </div>
                        <div class="form-group">
                            <label for="center-address" class="control-label"><i>*</i>地址</label>
                            <input type="text" class="form-control" id="center-address">
                        </div>
                        <span id="addText"></span>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default btn-333" data-dismiss="modal">取消</button>
                    <button id="add" type="button" class="btn btn-primary">确定</button>
                </div>
            </div>
        </div>
    </div>

    <!--修改弹框-->
    <div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                            aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="exampleModalLabel">修改用户信息</h4>
                </div>
                <div class="modal-body">
                    <form>
                        <input type="hidden" id="center-update-id"/>
                        <div class="form-group">
                            <label for="recipient-name" class="control-label"><i>*</i>干洗中心名称</label>
                            <input type="text" class="form-control" id="recipient-name">
                        </div>
                        <div class="form-group">
                            <label for="recipient-address" class="control-label"><i>*</i>地址</label>
                            <input type="text" class="form-control" id="recipient-address">
                        </div>
                        <span id="updateText"></span>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default btn-333" data-dismiss="modal">取消</button>
                    <button id="update" type="button" class="btn btn-primary">修改</button>
                </div>
            </div>
        </div>
    </div>
    <!--引入模态框和复选框js-->
    <script src="../../js/checkbox.js"></script>
    <script src="../../js/jquery.pagination.min.js"></script>
    <script>
        var totalPage = $("#totalPage").html();
        $("#pagination").pagination({
            totalPage: totalPage,
            isShow: true,
            count: totalPage,
            homePageText: "首页",
            endPageText: "尾页",
            prevPageText: "上一页",
            nextPageText: "下一页",
            callback: function (current) {
                var select_option = $(".select-option .order-num").val();
                var centerPage = {
                    currentPage: current ,
                    centerName: select_option,
                    size: 5,
                };

                if (select_option == "") {

                    $.ajax({
                        url: "/system/center",
                        contentType: "application/json; charset=utf-8",
                        type: "post",
                        data: JSON.stringify(centerPage),
                        success: function (data) {
                            $("#tb tr").html("");
                            var tb = "";

                            $.each(data.list, function (n, v) {

                                tb += "<tr id='tr'>" +
                                    "<td><input name='ck'type='checkbox' value='" + v.id + "'/></td>" +
                                    "<td>" + v.centerName + "</td>" +
                                    "<td>" + v.centerAddress + "</td><td>";
                                $.each(v.stores,function (a,b) {
                                    tb+="<span>"+b.storesName+"</span>&nbsp;";
                                })

                                 tb+= "</td><td><img onclick='lick(this,this.id)' src='../../images/delete02.png'/><img src='../../images/edit.png'data-toggle=\"modal\"\n" +
                                    "                            data-target='#exampleModal'" +
                                    "                            onclick='updateClick(this, this.id)' " +
                                    "                            data-whatever='修改洗衣门店信息'/></td>" +
                                    "</tr>"

                            });
                            $("#totalPage").html(data.totalPage);
                            $("#tb").html(tb)
                        }
                    });
                }
                ;
            },
        })

        function updateClick(obj, id) {

            $("#updateText").html("");
            var td_content = $(obj).parents("tr").children("td");  //获取当前行中的所有td值
            var id = td_content.eq(0).children("input").val(); //获取当前行第1个td的值
            var name = td_content.eq(1).text(); //获取当前行第2个td的值
            var address = td_content.eq(2).text(); //获取当前行第2个td的值


            document.getElementById('center-update-id').value = id;
            document.getElementById('recipient-name').value = name;
            document.getElementById('recipient-address').value = address;



        }

        function lick(obj, id) {


            var td_content = $(obj).parents("tr").children("td");  //获取当前行中的所有td值
            var ids = td_content.eq(0).children("input").val(); //获取当前行第1个td的值
            console.log(ids)
            $.ajax({
                url: "/system/del_ids",
                contentType: "application/json; charset=utf-8",
                type: "post",
                data: ids,
                success: function (data) {
                    if (data.code == "0") {
                        alert("删除成功!")
                        window.location.href = "/system/center";
                    } else {
                        alert("删除失败!");
                    }
                }
            });

        }

    </script>
</div>
</body>
</html>